.Net Core EF (Entity Framework v2.1)

This is an overview of EFCore 2.1 which contains many new features from EF6, such as lazy loading – which was not in EF Core until v2.1.

 

EF Core Migrations

 

 

EF Migrations

We can run EF Migrations through Visual Studio in the PM console or through a plain command line console using the dotnet cli. Below is an example of using both approaches.

 

PM> Add-Migration init
The EF Core tools version '2.1.1-rtm-30846' is older than that of the runtime '2.1.4-rtm-31024'. Update the tools for the latest features and bug fixes.
Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 2.1.4-rtm-31024 initialized 'DataContext' using provider 'Npgsql.EntityFrameworkCore.PostgreSQL' with options: None
To undo this action, use Remove-Migration.
PM> script-migration
The EF Core tools version '2.1.1-rtm-30846' is older than that of the runtime '2.1.4-rtm-31024'. Update the tools for the latest features and bug fixes.
Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 2.1.4-rtm-31024 initialized 'DataContext' using provider 'Npgsql.EntityFrameworkCore.PostgreSQL' with options: None
PM> update-database
The EF Core tools version '2.1.1-rtm-30846' is older than that of the runtime '2.1.4-rtm-31024'. Update the tools for the latest features and bug fixes.
Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 2.1.4-rtm-31024 initialized 'DataContext' using provider 'Npgsql.EntityFrameworkCore.PostgreSQL' with options: None
Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (51ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE c.relname='__EFMigrationsHistory');
Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (47ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      CREATE TABLE "__EFMigrationsHistory" (
          "MigrationId" character varying(150) NOT NULL,
          "ProductVersion" character varying(32) NOT NULL,
          CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY ("MigrationId")
      );
Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (28ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE c.relname='__EFMigrationsHistory');
Applying migration '20181114214052_init'.
Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (28ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT "MigrationId", "ProductVersion"
      FROM "__EFMigrationsHistory"
      ORDER BY "MigrationId";
Microsoft.EntityFrameworkCore.Migrations[20402]
      Applying migration '20181114214052_init'.
Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (34ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      CREATE TABLE ....
PM> 

 

Using dotnet CLI

EF Core interactions can be done through the CLI. The following command starts the EF tools CLI. This requires the Microsoft.EntityFrameworkCore.Tools.DotNet nuget package.

[john@macbook]$ dotnet ef

                     _/\__
               ---==/    \\
         ___  ___   |.    \|\
        | __|| __|  |  )   \\\
        | _| | _|   \_/ |  //|\\
        |___||_|       /   \\\/\\

Entity Framework Core .NET Command-line Tools 2.1.4-rtm-31024

Usage: dotnet ef [options] [command]

Options:
  --version        Show version information
  -h|--help        Show help information
  -v|--verbose     Show verbose output.
  --no-color       Don't colorize output.
  --prefix-output  Prefix output with level.

Commands:
  database    Commands to manage the database.
  dbcontext   Commands to manage DbContext types.
  migrations  Commands to manage migrations.

Use "dotnet ef [command] --help" for more information about a command.
[john@macbook]$ dotnet ef migrations add InitialMigration
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
 Entity Framework Core 2.1.4-rtm-31024 initialized 'DataContext' using provider 'Npgsql.EntityFrameworkCore.PostgreSQL' with options: None
Done. To undo this action, use 'ef migrations remove'
[john@macbook]$ dotnet ef database update
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
 Entity Framework Core 2.1.4-rtm-31024 initialized 'DataContext' using provider 'Npgsql.EntityFrameworkCore.PostgreSQL' with options: None
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
 Executed DbCommand (65ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
 SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid=c.relnamespace WHERE c.relname='__EFMigrationsHistory');

Summary of dotnet cli commands

  • dotnet ef add /remove
  • dotnet ef database update

 

 

EF Core Migrations and Automation

Setting up DB migrations in TFS. A sample from .NetConf2017 indicates the use of a TFS add-in called BDC. It does the following steps:

  • Update connection string
  • Deploy EF Core migrations
  • Update value
  • Copy files

 

 

 

 

References

DevOps with EF Core and TFS 2017
https://www.youtube.com/watch?v=gGtJ4pj-EbU

 

eof